#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
extern "C"{
  #include "lifegame.h"
}

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Create LifeGame, width=32, height=16, brith rate=40% 
LifeGame_T lg = lg_new(32,16,40);
void setup() {
  // put your setup code here, to run once:
  // I2C SDA<->GPIO0 SCL<->GPIO2 
  Wire.begin(0,2);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(10);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(1);
  display.setCursor(9,28);
  display.println("Life Game");
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();  
}

void loop() {
  // put your main code here, to run repeatedly:
  display.clearDisplay();
  unsigned char i,j;
  for(i=0;i<lg.w;++i){
    for(j=0;j<lg.h;++j){
      if(lg_live(&lg,i,j)) display.fillRect(i*4,j*4, 3, 3, 1);
    }
  }
  display.display();
  lg_evolution(&lg);
  delay(100);
}
